<?php

namespace App\Repository\Repositories;

use App\Repository\Models\Permission;
use App\Repository\Models\Action;
use App\Repository\Repositories\Interfaces\ActionRepository;

/**
 * Class ActionRepositoryEloquent.
 *
 * @package namespace App\Repository\Repositories;
 */
class ActionRepositoryEloquent extends BaseRepository implements ActionRepository
{
    /**
     * 搜索
     * @var array
     */
    protected $fieldSearchable = [
    ];

    /**
     * Specify Model class name
     *
     * @return string
     */
    public function model()
    {
        return Action::class;
    }

    /**

     * 批量存储action
     */
    public function multiSave($params)
    {


        $act_id=[];
        foreach($params as $k =>$v){
            $act_id[]=Action::query()->create($v);

        }
        return $act_id;

        // \DB::beginTransaction();
        // try {
        //     $save = \DB::table('actions')->insert($params);
            // \DB::commit();
        // } catch (\Exception $e) {
            // \DB::rollback(); //事务回滚;
            // return false;
        // }
//
//        foreach ($params as $key => &$value) {
//            $id = isset($value['id'])?$value['id']:null;
//            $value = $this->model->updateOrCreate(['id'=>$id],$value)->toArray();
//        }
//        return $params;

    }
    /**
     * 删除存储
     */
    public function deleteCondition($menu_id)
    {
        $this->applyCriteria();
        $this->applyScope();
        // \DB::beginTransaction();
        try {
            $this->model->where('menu_id', $menu_id)->delete();
            // \DB::table('actions')->where('menu_id', '=', $menu_id)->delete();
            // \DB::commit();
        } catch (\Exception $e) {
            // \DB::rollback(); //事务回滚;
            return false;
        }
        return true;
    }
    /**
     * @param $name
     * @param $display_name
     * @param $description
     * 添加权限
     */
    public function add_actionPermission($name,$display_name,$description){
        $data=[
            'name'=>$name,
            'display_name'=>$display_name,
            'description'=>$description,
            'type'=>'action'
        ];

        $perid=Permission::query()->create($data)->toArray();
        return $perid;
    }

    /**
     * @param $name
     * @param $display_name
     * @param $description
     * 添加权限
     */
    public function add_menuPermission($name,$display_name,$description){
        $data=[
            'name'=>$name,
            'display_name'=>$display_name,
            'description'=>$description,
            'type'=>'menu'
        ];

        $perid=Permission::query()->create($data)->toArray();
        return $perid;
    }
}
